<template>
	<view>
		<view class="common-wrap tp-w690">
			<view class="common_tit-wrap">
				<view class="common_tit-wrap tp-w690">
					<view class="l-wrap">接送信息</view>
					<view class="r-wrap"></view>
				</view>
			</view>

			<view class="input-wrap" :class="{disabled: curPickuperId}">
				<view class="type">接送人</view>
				<view class="input" @click="handleChoosePickuper">
					<input type="text" disabled :value="pickuperInfo.name" placeholder="请选择" />
					<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
				</view>
			</view>
			<!-- 
			<view class="input-wrap">
				<view class="type">接送服务</view>
				<view class="ul_service">
					<view class="li_service" v-if="goShow" @click="handleChooseServiceType('in')">
						<uni-icons v-if="pickuperInfo.serviceType==='in'" type="circle-filled" size="20" color="#FFAF3C"></uni-icons>
						<uni-icons v-else type="circle" size="20" color="#999"></uni-icons>
						<text class="txt">接站</text>
					</view>
					<view class="li_service" v-if="backShow" @click="handleChooseServiceType('out')">
						<uni-icons v-if="pickuperInfo.serviceType==='out'" type="circle-filled" size="20" color="#FFAF3C"></uni-icons>
						<uni-icons v-else type="circle" size="20" color="#999"></uni-icons>
						<text class="txt">送站</text>
					</view>
				</view>
			</view> -->

			<view class="input-wrap" v-if="goPickType != 3">
				<view class="type tp-mr15">接站地址</view>
				<view class="input" @click="handleChooseDestination('in')">
					<input type="text" disabled :value="pickuperInfo.in.destination" placeholder="请选择" />
					<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
				</view>
			</view>
			<view class="input-wrap" v-if="goPickType != 3 && pickuperInfo.in.waitingTime">
				<view class="type tp-mr15">接站候车时间</view>
				<view class="flex-1 flex flex_end">
					<text class="tp-fc666 tp-fz28">{{pickuperInfo.in.waitingTime || '--'}} 之前</text>
				</view>
			</view>
			<view class="input-wrap" v-if="backPickType != 3">
				<view class="type tp-mr15">送站地址</view>
				<view class="input" @click="handleChooseDestination('out')">
					<input type="text" disabled :value="pickuperInfo.out.destination" placeholder="请选择" />
					<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
				</view>
			</view>
			<view class="input-wrap" v-if="backPickType != 3 && pickuperInfo.out.waitingTime">
				<view class="type tp-mr15">送站候车时间</view>
				<view class="flex-1 flex flex_end">
					<text class="tp-fc666 tp-fz28">{{pickuperInfo.out.waitingTime || '--'}} 之前</text>
				</view>
			</view>
		</view>

		<view class="common-btn" @click="handleConfirmAddPickuperInfo">确定</view>
	</view>
</template>

<script>
	import {
		BD09ToGCJ02
	} from '../utils/gcoord.js'
	import {
		mapGetters
	} from 'vuex'
	import calendar from '@/components/tp-calendar/util'
	export default {
		data() {
			return {
				curPickuperId: 0, // 当前要编辑的接送人id
				serviceType: '', // 接送服务类型（去程|返程）
				pickuperInfo: {
					name: '', // 接送人名字
					in: {
						serviceType: 'in', // 接送服务 接站|送站(接站为目的地选择，送站为出发地选择)
						destination: '', // 目的地
					},
					out: {
						serviceType: 'out', // 接送服务 接站|送站(接站为目的地选择，送站为出发地选择)
						destination: '', // 目的地
					},
					// serviceType: 'in', // 接送服务 接站|送站(接站为目的地选择，送站为出发地选择)
					// destination: '', // 目的地
				},
				busInfo: {}, // 班车信息
				// vip: false, // 是否商务班车
				// goIsPickUp: '0', // 去程可接送 0.可 1.不可
				// backIsPickUp: '0', // 返程可接送  0.可 1.不可

				// goPickUpTypeStart: '-1', // 去程出发站接送类型 -1.无接送 0.接站
				// goPickUpTypeEnd: '-1', // 	去程终点站接送类型 -1.无接送 1.送站
				// backPickUpTypeStart: '-1', // 返程出发站接送类型 -1.无接送 0.接站	
				// backPickUpTypeEnd: '-1', // 返程终点站接送类型 -1.无接送 1.送站

				goPickType: 0, // 	出发站接送类型 1.固定点 2.范围点 3.不接送
				backPickType: 0, // 终点站接送类型 1.固定点 2.范围点 3.不接送
			};
		},
		onLoad(opt) {
			this.$store.commit('delCurPickuperInfo')
			this.curPickuperId = opt && opt.id
			// this.goIsPickUp = opt && opt.goIsPickUp
			// this.backIsPickUp = opt && opt.backIsPickUp

			// this.goPickUpTypeStart = opt && opt.goPickUpTypeStart
			// this.goPickUpTypeEnd = opt && opt.goPickUpTypeEnd
			// this.backPickUpTypeStart = opt && opt.backPickUpTypeStart
			// this.backPickUpTypeEnd = opt && opt.backPickUpTypeEnd

			this.serviceType = opt && opt.serviceType
			this.busInfo = opt && opt.busInfo && JSON.parse(opt.busInfo)
			
			this.judgeVip()
			this.$store.commit('delPickuperChoosedAddressObj')
		},
		onShow() {
			this.getCurPickuperInfo()
		},
		computed: {
			...mapGetters({
				curPickuperInfo: 'getCurPickuperInfo'
			})
		},
		methods: {
			handleChoosePickuper(e) {
				if (this.curPickuperId) return false
				uni.navigateTo({
					url: './pickuperList.toAndFor?serviceType=' + this.serviceType
				});
			},
			handleChooseDestination(type) { // 选择目的地
				// 此处选择的车的类型，去判断就去哪个地图（比如商务车去vip地图）
				// let vip = this.vip
				// let {
				// 	serviceType
				// } = this.pickuperInfo
				// if (vip) {
				// 	uni.navigateTo({
				// 		url: './busVipMap?travelsStatus=to'
				// 	});
				// } else {
					if (type === 'in') { // 接站为目的地选择
						uni.navigateTo({
							url: './busStationMap?travelsStatus=to&goPickType=' + this.goPickType + '&busInfo=' + JSON.stringify(this.busInfo)
						});
					} else if (type === 'out') { // 送站为出发地选择
						uni.navigateTo({
							url: './busStationMap?travelsStatus=from&backPickType=' + this.backPickType + '&busInfo=' + JSON.stringify(this.busInfo)
						});
					}
				// }
			},
			// handleChooseServiceType(type) { // 选择接送服务 接站|送站(接站为目的地选择，送站为出发地选择)
			// 	this.pickuperInfo.serviceType = type
			// 	if (this.pickuperInfo.destination) { // 如果已经选择了目的地，切换接送站的时候，重置目的地为空
			// 		this.pickuperInfo.destination = ''
			// 	}
			// },
			handleConfirmAddPickuperInfo() { // 提交接送人的信息
				let arr = this.curPickuperInfo // 当前所选的接送人
				let picArr = this.$store.getters.getPickuperListToAndFor[this.serviceType] || []
				let arr1 = arr.map((item, index) => { // 处理本次选择的这几个数据 添加地址
					item = { ...item,
						...this.pickuperInfo
					}
					return item
				})
				picArr.forEach(pic => {
					arr1.forEach(item => {
						if (item.id == pic.id) {
							// pic=item
							Object.assign(pic, item)
						}
					})
				})
				this.$store.commit('upPickuperListToAndFor', {
					[this.serviceType]: picArr
				})
				this.$store.commit('upCurPickuperInfo', arr1)
				// arr1.forEach(item => {
				// 	this.$store.commit('upCurPickuperInfo', { ...item,
				// 		...this.pickuperInfo
				// 	})
				// 	this.$store.commit('upPickuperList', { ...item,
				// 		...this.pickuperInfo
				// 	})
				// })
				// obj = {...this.curPickuperInfo, ...this.pickuperInfo}
				if (arr1.length == 0) {
					return uni.showToast({
						title: '请选择接送人',
						icon: 'none'
					})
				}
				if (!this.pickuperInfo.in.destination && !this.pickuperInfo.out.destination) {
					return uni.showToast({
						title: '请选择目的地',
						icon: 'none'
					})
				}
				// this.$store.commit('upCurPickuperInfo', obj)

				// let arr = this.$store.getters.getPickuperListToAndFor[this.serviceType] || []
				// let f = arr.findIndex((el) => el.id == obj.id)
				// let arrIndex = [] // index  元数据数据相同的小标
				// let arr1Index = [] // index 选择的数据相同的下标
				// arr1.forEach((a1,i1) => {
				// 	arr.forEach((a2,i2) => {
				// 		if(a1.id == a2.id){
				// 			arr1Index.push(i1)
				// 			arrIndex.push(i2)
				// 		}
				// 	})
				// })
				// console.log(arr1Index);
				// console.log(arrIndex);
				// if (arrIndex.length != 0) {
				// 	for(let i = 0; i < arrIndex.length; i++){
				// 		arr[arrIndex[i]] = arr1[arr1Index[i]]
				// 	}
				// }

				// this.$store.commit('upPickuperListToAndFor', {
				// 	[this.serviceType]: arr
				// })
				uni.navigateBack({
					delta: 1
				});
			},
			getCurPickuperInfo() { // 获取当前所选择的接送人信息
				if (this.curPickuperId) {
					let arr = this.$store.getters.getPickuperListToAndFor
					arr[this.serviceType].forEach(item => {
						if (item.id == this.curPickuperId) {
							this.pickuperInfo = item
							this.pickuperInfo.name = item.passengerName
							this.$store.commit('upCurPickuperInfo', [item])
						}
					})
				} else {
					let arr = []
					this.curPickuperInfo.forEach(item => {
						arr.push(item.passengerName)
					})
					this.pickuperInfo.name = arr.join(',')
				}
				let obj = this.$store.getters.getPickuperChoosedAddressObj
				// if (obj.from || obj.to) {
				// 	this.pickuperInfo.destination = obj.from && obj.from.address || obj.to && obj.to.address
				// 	this.pickuperInfo.destinationObj = obj.from || obj.to
				// 	this.$store.commit('delPickuperChoosedAddressObj')
				// }
				console.log(obj)
				if (obj.from) { // 送站
					this.pickuperInfo.out.destination = obj.from && obj.from.address
					this.pickuperInfo.out.destinationObj = obj.from
					
					if (obj.from.transferTime) {
						let outTime = Number(obj.from.transferTime) * 1000 * 60 + Number(this.busInfo.travelTime) * 1000 * 60
						this.pickuperInfo.out.waitingTime = this.format(calendar.transdate(this.busInfo.startTime) * 1000 + outTime)
					}
					// this.$store.commit('delPickuperChoosedAddressObj')
				}
				if (obj.to) { // 接站
					this.pickuperInfo.in.destination = obj.to && obj.to.address
					this.pickuperInfo.in.destinationObj = obj.to
					
					if (obj.to.transferTime) {
						let inTime = Number(obj.to.transferTime) * 1000 * 60;
						this.pickuperInfo.in.waitingTime = this.format(calendar.transdate(this.busInfo.startTime) * 1000 - inTime)
					}
				}
			},
			// 时间戳转日期
			add0(m) {
				return m < 10 ? '0' + m : m
			},
			format(shijianchuo) {
				//shijianchuo是整数，否则要parseInt转换
				var time = new Date(shijianchuo);
				var y = time.getFullYear();
				var m = time.getMonth() + 1;
				var d = time.getDate();
				var h = time.getHours();
				var mm = time.getMinutes();
				var s = time.getSeconds();
				return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
			},
			judgeVip() {
				let type = this.pickuperInfo.serviceType == 'out' ? 0 : 1
				let {
					schedulingId,
					lineId
				} = this.busInfo
				this.$http.get(this.$api.getPickuperStationList + '?clientType=miniapp&schedulingId=' + schedulingId + '&lineId=' +
					lineId + '&type=' +
					type).then(res => {
					if (res.code === 200) {
						this.goPickType = res.data.goPickType
						this.backPickType = res.data.backPickType
						// if (this.goPickType == 3) {
						// 	this.pickuperInfo.serviceType = 'out'
						// }
						// res.data.map(item => {
						// 	let result = BD09ToGCJ02({
						// 		latitude: item.lat - 0,
						// 		longitude: item.lng - 0
						// 	})
						// 	item.lat = item.latitude = result[1] + ''
						// 	item.lng = item.longitude = result[0] + ''
						// 	item.iconPath = '/static/imgs/icons/bus1-icon.png'
						// 	item.callout = {
						// 		content: item.stationName,
						// 		display: 'ALWAYS'
						// 	}
						// })
						// this.vip = res.data.length === 0 ? true : false
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.common-wrap {
		border-radius: 12rpx;
		background-color: #fff;
		margin-top: 20rpx;
		padding: 30rpx 30rpx 0;
		box-sizing: border-box;
	}

	.input-wrap {
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #ebebeb;

		&:last-child {
			border: none;
		}

		&.disabled {
			color: #999;
		}

		.ul_service {
			display: flex;

			.li_service {
				display: flex;
				align-items: center;
				font-size: 30rpx;
				margin-left: 80rpx;

				.txt {
					margin-left: 20rpx;
				}
			}
		}

		.type {
			font-size: 30rpx;
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: 30rpx;
			display: flex;

			input {
				padding-right: 6rpx;
			}
		}
	}

	.common-btn {
		width: 580rpx;
		box-shadow: none;
		margin-top: 80rpx;
	}
</style>
